<?php

require_once(dirname(__FILE__) . '/appconf.php');

class Test extends Site {

    function setup() {
        $this->run_modes('test');
        $this->start_mode('test');
        $this->selfurl = '/testxml.php';
        parent::setup();
    }

    function cgiapp_prerun() {

        parent::cgiapp_prerun();
    }

    function action_test() {

        $submited = $this->get_q("submited", 0, "int");
        if ($submited) {

            $url = $this->get_q("url");
            $xmlObj = simplexml_load_file($url);

            if ($xmlObj) {
                foreach ($xmlObj as $value_row) {

                    $p_num = db_query_count('fg_product', "pnum=" . mysql_escape_string($value_row->pnum) . " ");
                    if ($p_num > 0)
                        continue;


                    $insert_array = array("num", "topic", "bname", "pnum", "pname", "price", "volume", "fun", "how", "ps", "udate", "niceimg", "imgurl", "topicurl", "brandurl", "resStandard", "resCondition", "amount", "resamount", "resdates", "resdatee", "backvote", "resShowDate", "item1amount", "item2amount", "item3amount", "item4amount", "item5amount", "item1grade", "item2grade", "item3grade", "item4grade", "item5grade", "item1apercent", "item2apercent", "item3apercent", "item4apercent", "item5apercent", "vote_option_json");

                    $insert = array();
                    foreach ($insert_array as $insert_value) {

                        $insert[$insert_value] = $this->xml2Str($value_row->$insert_value);
                    }


                    db_add('fg_product', $insert);
                    echo $value_row->num . "<br>";
                }
            }
        } else {
            echo
            '
                <form method="post" action="">
                        <input type="text" name="url" size=80 />
                        <input type="hidden" name="submited" value=1   />
                        <input type="submit" />
                </form>';
        }
    }

    function xml2Str($obj) {
        return trim((string) $obj);
    }

}

header("Content-type: text/html; charset=utf-8");
$webapp = new Test();
$webapp->run();
?>